package com.bionic.banking.commands;

import com.bionic.banking.auth.manager.Config;
import com.bionic.banking.core.entity.*;
import com.bionic.banking.dao.connection.LoggerLoader;
import com.bionic.banking.logic.DomainUtil;
import com.bionic.banking.logic.controller.*;
import com.bionic.banking.logic.exception.InvalidParameterException;
import java.io.*;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class CommandCreateCustomer implements ICommand {

    private static final org.apache.log4j.Logger logger =
            LoggerLoader.getLogger(CommandCreateCustomer.class);

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        String page = null;
        try {
            //Get the data from user's form
            String login = (String) request.getParameter("login");
            String password = (String) request.getParameter("password");
            String firstName = (String) request.getParameter("firstName");
            String lastName = (String) request.getParameter("lastName");
            String middleName = (String) request.getParameter("middleName");
            Integer age = Integer.valueOf(request.getParameter("age"));
            Boolean gender = Boolean.valueOf(request.getParameter("gender"));
            String phone = (String) request.getParameter("phone");
            String fax = (String) request.getParameter("fax");
            String email = (String) request.getParameter("email");
            String imageBuffStr = (String) request.getParameter("imageBuffer");           
            //address
            String postalCode = (String) request.getParameter("postalcode");
            String country = (String) request.getParameter("country");
            String region = (String) request.getParameter("region");
            String city = (String) request.getParameter("city");
            String street = (String) request.getParameter("street");
            String line1 = (String) request.getParameter("line1");
            String line2 = (String) request.getParameter("line2");

            //1.1 Create a user entity            
            User user = new User(login, password);
            UserController userbean = new UserController();
            //create a new user in  db
            if (userbean.create(user)) {
                //1.2 Create an address entity
                Address address = new Address(postalCode, country, region, city, street, line1, line2);
                //save address to db
                AddressController addressbean = new AddressController();
                addressbean.create(address);

                //1.3 Create a customer entity and attsch user and addres 
                Customer entity = new Customer(firstName, lastName, middleName, gender, age, phone, fax,
                        email, user.getUserId());

                if (address.getAddressId() > 0) {
                    entity.setAddressId(address.getAddressId());
                }

                if (!imageBuffStr.equals("")) {
                     byte[] imageBytes = DomainUtil.stringToBuffer(imageBuffStr);
                    //byte[] imageBytes = DomainUtil.fileToByteArray(photoFile);
                    entity.setImage(imageBytes);
                }

                CustomerController bean = new CustomerController();

                try {
                    if (bean.create(entity)) {
                        //request.setAttribute("customer", customer);
                        request.setAttribute("content", Config.getInstance().getProperty(Config.CUSTOMERLIST));
                        page = Config.getInstance().getProperty(Config.MAIN);
                    } else {
                        page = Config.getInstance().getProperty(Config.ERROR);
                    }
                } catch (SQLException | InvalidParameterException ex) {
                    logger.error(ex);
                    request.setAttribute("error", ex.getMessage());
                    page = Config.getInstance().getProperty(Config.ERROR);
                }
            }
        } catch (NumberFormatException | InstantiationException |
                InvalidParameterException | UnsupportedEncodingException |
                IllegalAccessException | SQLException ex) {
            logger.error(ex);
            request.setAttribute("error", ex.getMessage());
            page = Config.getInstance().getProperty(Config.ERROR);
        }

        return page;
    }
}
